﻿using BaseAPI;
using MemorySiteServer.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace MemorySiteServer.VIewModels.Account
{
    public class LoginRequest: BaseGetOneViewModel<User>
    {
        [JsonIgnore]
        [Obsolete]
#pragma warning disable CS0809 // 过时成员重写未过时成员
        public override long ID { get; set; }
#pragma warning restore CS0809 // 过时成员重写未过时成员
        /// <summary>
        /// 用户名
        /// </summary>
        [Required]
        [StringLength(20,MinimumLength = 3)]
        public string Name { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        [Required]
        [StringLength(32, MinimumLength = 3)]
        public string Password { get; set; }
        /// <summary>
        /// 验证码
        /// </summary>
        [Required]
        [StringLength(4,MinimumLength =4)]
        public string Code { get; set; }
        /// <summary>
        /// 客户端ID
        /// </summary>
        [Required]
        public string ClientID { get; set; }

        /// <summary>
        /// 按用户名查找用户，级联角色和权限
        /// </summary>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public override IQueryable<User> QueryFun(IQueryable<User> queryable)
        {
            return queryable.Where(x => x.Name == Name && !x.IsDeleted).
                Include(x => x.Role).
                ThenInclude(x => x.RolePermissions);
        }

        /// <summary>
        /// 验证用户名密码，通过返回True
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool VerifyLogin(User user)
        {
            return Name == user.Name && Password == user.Password;
        }
    }
}
